/**
 * ManPieceController.java
 * 10/05/2011 17:28:33
 */
package controller;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import model.memento.MovementDirection;
import model.pieces.Man;

/**
 * @author walber
 * 
 */
public class ManPieceController implements KeyListener {

	private final Man manPiece;

	public ManPieceController(Man man) {
		this.manPiece = man;
	}

	@Override
	public void keyPressed(KeyEvent e) {
		MovementDirection direction = getDirection(e);
		if (direction != null) {
			fireMovement(direction);
		}
	}

	@Override
	public void keyReleased(KeyEvent e) {
	}

	@Override
	public void keyTyped(KeyEvent e) {
	}

	private void fireMovement(MovementDirection direction) {
		manPiece.move(direction);
	}

	private static MovementDirection getDirection(KeyEvent e) {
		switch (e.getKeyCode()) {
		case KeyEvent.VK_UP:
			return MovementDirection.UP;
		case KeyEvent.VK_RIGHT:
			return MovementDirection.RIGHT;
		case KeyEvent.VK_DOWN:
			return MovementDirection.DOWN;
		case KeyEvent.VK_LEFT:
			return MovementDirection.LEFT;
		default:
			return null;
		}
	}
}
